package klk.shop.objects;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import klk.shop.utilities.MysqlUtilities;


public class Country extends KLKObject {

	
	protected int id;
	protected String name;
	
	
	public int getId() {
		return id;
	}
	public boolean setId(int id) {
		this.id = id;
		return true;
	}
	
	
	public String getName() {
		return name;
	}
	public void setName(String Name) {
		this.name = Name;
	}
	
	
	public Country(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Country() {
		this( -1, "" );
	}
	
	@Override
	public String toString() {
		return name;
	}
	
	public static Country getCountryFromDatabase(int countryId, String localeString) {
		Country country = new Country();
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
				
		try	{
			
			conn = MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			rs = stmt.executeQuery(
				"SELECT " +
					"id, " + 
					"name_" + localeString + " " +  
				"FROM " +
					"countries " +
				"WHERE id="+countryId+"; "
			);
			
			rs.next();
			
			country.id = rs.getInt("id");
			country.name = rs.getString("name_"+localeString);
			
		} catch (Exception e) {
			e.printStackTrace();
			country=null;
		} finally {
			if (rs != null) try { rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
		
		return country;	
	}
	
	@Override
	public String toHtml() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
